namespace WeakObject.Example1;

public class Program
{
    public static async Task Main()
    {
        Console.WriteLine("\nWeakReference 与强引用的区别:");
        object? strongRef = new();
        WeakReference weakRef = new(strongRef);
        Console.WriteLine($"创建后 - 强引用存在: {strongRef is not null}, 弱引用IsAlive: {weakRef.IsAlive}");
        strongRef = null;
        int index = 0;
        while (true)
        {
            GC.Collect();
            Console.WriteLine($"第{index++}次");
            await Task.Delay(1000);
            Console.WriteLine($"当前情况 - 强引用存在: {strongRef is not null}, 弱引用IsAlive: {weakRef.IsAlive}");
            if (!weakRef.IsAlive) return;
        }
    }
}